package com.first.agent.service.impl;

import com.first.agent.common.JsonResult;
import com.first.agent.common.R;
import com.first.agent.dao.RoleDao;
import com.first.agent.dao.impl.RoleDaoImpl;
import com.first.agent.pojo.Role;
import com.first.agent.service.RoleService;

import java.sql.SQLException;
import java.util.Date;
import java.util.List;

public class RoleServiceImpl implements RoleService {
    private RoleDao roleDao = new RoleDaoImpl();

    @Override
    public R roleMod(String role_name, Integer status, String role_id) {
        R r = new R();
        try {
            int result = roleDao.roleMod(role_name, status, role_id);
            if (result>0){
                r.setCode(200);
                r.setMsg("修改成功");
            }
        } catch (SQLException e) {
            e.printStackTrace();
            r.setCode(500);
            r.setMsg("系统开小差了，请联系管理员");
        }

        return r;
    }

    @Override
    public R roleAdd(String role_name, Integer status) {
        R r = new R();
        try {
            int result = roleDao.roleAdd(role_name, status);
            if (result>0){
                r.setCode(200);
                r.setMsg("添加成功");
            }
        } catch (SQLException e) {
            e.printStackTrace();
            r.setCode(500);
            r.setMsg("系统开小差了，请联系管理员");
        }
        return r;
    }

    @Override
    public R roleDel(String role_name) {
        R r = new R();
        try {
            int result = roleDao.roleDel(role_name);
            if (result>0){
                r.setCode(200);
                r.setMsg("删除成功");
            }
        } catch (SQLException e) {
            e.printStackTrace();
            r.setCode(500);
            r.setMsg("系统开小差了，请联系管理员");
        }
        return r;
    }

    @Override
    public JsonResult queryRoleList(Integer currentPage, Integer pageSize) {
        JsonResult jsonResult = new JsonResult();
        try {
            jsonResult.setCode(0);//查询成功响应码-0：layui动态查询数据渲染到表格中，规定响应码必须规定
            jsonResult.setMsg("查询成功");
            Integer totalSize = roleDao.countRole();
            jsonResult.setCount(totalSize);
            Integer param01 = (currentPage-1)*pageSize;
            Integer param02 = pageSize;
            List<Role> employeeList = roleDao.queryRoleList(param01, param02);
            jsonResult.setData(employeeList);
        } catch (SQLException e) {
            e.printStackTrace();
            jsonResult.setCode(500);
            jsonResult.setMsg("系统开小差，请联系管理员");
        }
        return jsonResult;
    }

}
